﻿using System.Collections.Generic;
using UnityEngine;
using Network;

public class InputModule:Singleton<InputModule>
{
    public Dictionary<int, uint> PlayerToEntityID = new Dictionary<int, uint>();

    public InputModule()
    {
        NetManager.Instance.RegistUdpMsg(OpCode.operation_move, onOperationMove);
    }

    public void OnMoveJoystick(Vector2 dir)
    {
        operation_move proto = new operation_move();
        proto.player_id = Mainloop.Instance.MyPlayerID;
        proto.direction = new vector2();
        proto.direction.x = dir.x; proto.direction.y = dir.y;
        NetManager.Instance.SendGameMessage(OpCode.operation_move, ProtoUtil.SerializeProto(proto), true);
    }

    void onOperationMove(MsgObject msg)
    {
        operation_move proto = ProtoUtil.DeserializeProto<operation_move>(msg.msgBody);
        int playerID = proto.player_id; uint entityID = PlayerToEntityID[playerID];
        Logic logic = GameManager.Instance.GetEntity(entityID);
        LogicMoveComponent lmove = logic.GetLogicComponent<LogicMoveComponent>();

        Transform mainCameraTransform = Camera.main.transform;
        Vector3 forward = mainCameraTransform.forward; forward.y = 0;
        Vector3 right = Vector3.Cross(Vector3.up, forward);
        Vector3 move = forward * proto.direction.y + right * proto.direction.x;
        lmove.LogicMove(move.normalized);
    }
}
